//
//  ZYEmotionTools.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/5.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit


//显示的列数
let EmotionMaxCol = 7
//显示的行数
let EmotionMaxRow = 3
//显示的个数
let EmotionMaxCount = EmotionMaxCol * EmotionMaxRow - 1



class ZYEmotionTools: NSObject {
    
    //创建的单例类可以重复的来使用,设置全局访问点
    static let sharedTools: ZYEmotionTools = ZYEmotionTools()
    
    //1.获取bundle
    lazy var emotionBundle: NSBundle = {
        
        //获取bundle的路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path: path)!
        
    }()
    
    //3  -----   Default表情的一维数组
    lazy var defaultEmotions:[ZYEmotionModel] = {
        
        return self.getEmotions("default/")
        
    }()
    
    //  ------  emoji表情的一维数组
    lazy var emojiEmotions:[ZYEmotionModel] = {
        
        return self.getEmotions("emoji/")
    
    }()
    
    // ------  浪小花表情的一维数组
    lazy var lxhEmotions:[ZYEmotionModel] = {
        
        return self.getEmotions("lxh/")
    }()
    
    //------------三维表情数组------------------
    lazy var allEmotion:[[[ZYEmotionModel]]] = {
        
        return [
        
            
            
            self.getEmotionGroup(self.defaultEmotions),
            self.getEmotionGroup(self.emojiEmotions),
            self.getEmotionGroup(self.lxhEmotions)
    
        ]
    
    }()
    
    
    //2.转成Emotion的是一维数组
    func getEmotions(fileName: String) -> [ZYEmotionModel]{
        
        //路径
        let filePath = emotionBundle.pathForResource("\(fileName)info.plist", ofType: nil)!
        
        //将plist转换成数组
        let array = NSArray(contentsOfFile: filePath)!
        
        //创建一个可变的临时数组,类型是字典类型
        var tempArray:[ZYEmotionModel] = [ZYEmotionModel]()
        
        //遍历array
        for dic in array {
            
            let e = ZYEmotionModel(dict: dic as![String: AnyObject])
            
            //----------7.5-----------
            e.path = "\(fileName)"
            
            tempArray.append(e)
            
        
        }
        return tempArray
    }
    
    /*
    目前有一维数组
    - 默认表情  一共108个
    - emoji表情 一共80个
    - 浪小花    一共40个
    
    把以上的一维数组转成二维数组
    默认表情
    [20 20 20 20 20 8] 0 -19  20- 39 ..... 100- 107
    emoji表情
    [20 20 20 20 ] 0-19 20 -39 40 -59
    浪小花
    [20 20]
    
    */
    //将一维数组转化成二维数组,需要截取数组,传入一维数组,指定类型
    func getEmotionGroup(emotion: [ZYEmotionModel]) -> [[ZYEmotionModel]]{
        
        //计算表情的页数
        //emotion.count 表示的是其中每一个一维数组的个数是多少
        let pageCount = (emotion.count + EmotionMaxCount - 1) / EmotionMaxCount
        
        //创建一个临时的可变数组,用来保存二维数组
        var tempArray:[[ZYEmotionModel]] = [[ZYEmotionModel]]()
        
        for i in 0..<pageCount {
            //位置和长度
            let loc = EmotionMaxCount * i
            var len = EmotionMaxCount
            
            //---------防止默认表情越界-------------
            if loc + len > emotion.count {
            
                len = emotion.count - loc
            }
            
            
            //截取的范围
            let range = NSMakeRange(loc, len)
            //数组的截取
            let arr = (emotion as NSArray).subarrayWithRange(range) as![ZYEmotionModel]
            //保存一维数组
            tempArray.append(arr)
        
        }
        //返回二维数组
        return tempArray
        
    }
    
    //7.7
    func getSearchEmotion(chs: String) -> ZYEmotionModel? {
        
        //遍历默认
        for e in defaultEmotions{
        
            if e.chs == chs {
            
                return e
            }
        }
    
        for e in lxhEmotions {
            
            //如果chs相等的话就返回图片
            if e.chs == chs {
                return e
            }
        }
        
        return nil
        
    }
    
    
}



extension ZYEmotionTools {
    
    func demo2() {
    /*
        
        //遍历一维数组,截取一维数组
        for (i, emo) in emotion.enumerate() {
        
        let loc =
        let len = EmotionMaxCount
        //1.首先要有一个截取的范围,需要借助OC的方法
        let range = NSMakeRange(<#T##loc: Int##Int#>, <#T##len: Int##Int#>)
        //2.进行数组的截取
        (emotion as NSArray).subarrayWithRange(<#T##range: NSRange##NSRange#>)
        
        }
 
    */
    
    }

}





























